//
//  CarInputChooseSeriesVC.m
//  New_Runway
//
//  Created by mack on 2018/3/15.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CarInputChooseSeriesVC.h"
#import "CarSeriesModel.h"
#import "CarInputChooseModelVC.h"
#import "CarInputChooseModel.h"

@interface CarInputChooseSeriesVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;

@end

@implementation CarInputChooseSeriesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"车系";
    self.view.backgroundColor= kColorWhite();
    [self initTableview];
//    [self loadCarSeries:self.carBrandId];
    [self loadCarSeries:self.model.brandId];
    
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    //    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    self.tableview.tableFooterView = [[UIView alloc] init];
    
    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.seriesArray.count ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    cell.tintColor = MAIN_NAV_COLOR();
    
    cell.textLabel.textColor = MAIN_FONT_COLOR();
    [cell.textLabel setFont:kFontSize(14)];
    seriesResult *seriesInfo = self.seriesArray[indexPath.row];
    cell.textLabel.text = seriesInfo.seriesName;
    if (self.modelSeleIndexPath == indexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.textLabel.textColor = MAIN_NAV_COLOR();
        
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    UITableViewCell *celled = [tableView cellForRowAtIndexPath:self.modelSeleIndexPath];
    celled.accessoryType = UITableViewCellAccessoryNone;
    celled.textLabel.textColor = MAIN_FONT_COLOR();
    //记录当前选中的位置索引
    self.modelSeleIndexPath = indexPath;
    //当前选择的打勾
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.textLabel.textColor = MAIN_NAV_COLOR();
    [self.tableview reloadData];
    [self cellDidClickWithIndex:indexPath];

    
}

- (void)cellDidClickWithIndex:(NSIndexPath *)indexPath {
    
    seriesResult *seriesInfo = self.seriesArray[indexPath.row];
    CarInputChooseModelVC *modelVC = [[CarInputChooseModelVC alloc] init];
    
    self.model.seriesName = seriesInfo.seriesName;
    self.model.seriesId = seriesInfo.seriesId;
    modelVC.fromControllerClass = self.fromControllerClass;
    modelVC.model = self.model;
    
    if ([self.fromControllerClass isEqual:NSClassFromString(@"NRWCarSubscriptionVC")]) {
        [self.navigationController safePopViewControllerWithClass:self.fromControllerClass];
        NSNotification *notificatiom = [NSNotification notificationWithName:@"ChooseCar" object:self.model userInfo:nil];
        [[NSNotificationCenter defaultCenter] postNotification:notificatiom];
        return;
    }

    [self.navigationController pushViewController:modelVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadCarSeries:(NSInteger)brandId{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,carSeriesApi];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:@{@"brandId":@(brandId)} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        CarSeriesModel *model = [CarSeriesModel mj_objectWithKeyValues:response];
        if (model.code == 1) {
            [self.seriesArray removeAllObjects];
            for (NSDictionary *dic in model.result) {
                seriesResult *seriesInfo = [seriesResult mj_objectWithKeyValues:dic];
                [self.seriesArray addObject:seriesInfo];
            }
            
        }
        [self.tableview reloadData];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
}
-(NSMutableArray *)seriesArray{
    if (!_seriesArray) {
        _seriesArray = [[NSMutableArray alloc] init];
    }
    return _seriesArray;
    
}

@end
